package com.easyeventlay.method;

import java.lang.reflect.InvocationTargetException;

public class Method {
    private Object instance;
    private java.lang.reflect.Method method;

    public Method(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
        try {
            this.method = clazz.getDeclaredMethod(methodName, parameterTypes);
            this.method.setAccessible(true);
        } catch (SecurityException | NoSuchMethodException var5) {
            var5.printStackTrace();
            throw new AppException("找不到method");
        }
    }

    public Method(Object instance, String methodName, Class<?>... parameterTypes) {
        this.instance = instance;

        try {
            this.method = instance.getClass().getDeclaredMethod(methodName, parameterTypes);
            this.method.setAccessible(true);
        } catch (SecurityException | NoSuchMethodException var5) {
            var5.printStackTrace();
            throw new AppException("找不到method");
        }
    }

    public Object invoke(Object... args) {
        Object returnVal = null;

        try {
            System.out.println("处理成功");
            returnVal = this.method.invoke(this.instance, args);
            return returnVal;
        } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException var4) {
            var4.printStackTrace();
            throw new AppException("method调用失败");
        }
    }

    public String toString() {
        return this.method.toString();
    }

    public boolean equals(Object obj) {
        if (!(obj instanceof Method)) {
            return false;
        } else {
            Method otherMethod = (Method)obj;
            if (this.instance == null) {
                return otherMethod.getInstance() == null && this.method.equals(otherMethod.getMethod());
            } else {
                return this.instance.equals(otherMethod.getInstance()) && this.method.equals(otherMethod.getMethod());
            }
        }
    }

    public Object getInstance() {
        return this.instance;
    }

    public java.lang.reflect.Method getMethod() {
        return this.method;
    }

    public void setInstance(Object instance) {
        this.instance = instance;
    }

    public void setMethod(java.lang.reflect.Method method) {
        this.method = method;
    }
}
